package com.myres.service;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.myres.model.Product;
import com.myres.product.db.ProductService;

public class UpdateProduct extends ServiceServlet {
   
	/**
	 * Constructor of the object.
	 */
	public UpdateProduct() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doRequest(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doRequest(request,response);
	}
	public void doRequest(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
		
		request.setCharacterEncoding("GBK");
		
		Enumeration params= request.getParameterNames();
		
		Product p=new Product();
		
		while(params.hasMoreElements()){
			String paramName=(String)params.nextElement();
		   if(paramName.equals("id")) p.setId(Integer.parseInt(request.getParameter(paramName)));
		   else if(paramName.equals("price")) p.setPrice(Float.parseFloat(request.getParameter(paramName)));
		   else if(paramName.equals("serial")) p.setSid(Integer.parseInt(request.getParameter(paramName)));
		   else if(paramName.equals("state")) p.setState(Integer.parseInt(request.getParameter(paramName)));
		   else if(paramName.equals("recommend")) p.setOnRecommend(Byte.parseByte(request.getParameter(paramName)));
		   else if(paramName.equals("onIndex")) p.setOnIndex(Byte.parseByte(request.getParameter(paramName)));
		   else if(paramName.equals("productName")){
			   String n=request.getParameter(paramName);
			   System.out.println(n);
			   String m=java.net.URLDecoder.decode(n,"utf-8");
			   System.out.println(m);
		
			   p.setName(m);
		   }
		}
        ProductService service=new ProductService();
       int count= service.updateProduct(p);
       if(count==1){
    	   response.setStatus(200);
       }else{
    	   response.setStatus(505);
       }
      //  service.updateProduct(table, fields, where, id);
     }
	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
